# -*- coding: utf-8 -*-
'''
Created on 2012-9-25

@author: null
'''
from flask import Blueprint, render_template, request, jsonify, session
from services import ThingService
from models import Thing
from common.auth import requires_sign_in
from flaskext.babel import gettext


thing_views = Blueprint("thing", __name__); 

@thing_views.route("/user_id=<int:user_id>&limit=<int:limit>", defaults={"limit": 10})
@requires_sign_in
def index(user_id, limit):
    thingService = ThingService();
    result = thingService.get_thing_list(user_id, limit);
    return render_template("home.html", things=result);

@thing_views.route("/add", methods=["POST", "GET"])
@requires_sign_in
def add():
    if request.method == "POST":
        description = request.form.get("thing_description", None);

        if not description:
            raise Exception(gettext("Description cannot be empty."));
        thing = Thing();
        thing.description = description;
        thing.user_id = session.get("userid", None);
    
        thingService = ThingService();
        new_id = thingService.add(thing);
        return jsonify({"status": "OK", "new_id": new_id});
    else:
        return render_template("/thing/add.html");
    
    